home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 22 code / Paper Juggling / Shell Files / DialogUtils.c < prev    next >
Encoding:
Text File  |  1995-03-16  |  5.2 KB  |  212 lines  |  [TEXT/MMCC]

  1. //---------------------------------------------------------------------
  2. //---------------------------------------------------------------------
  3. //
  4. //    Horrible Rickety Shell, by Dave Johnson
  5. //
  6. //    © Copyright 1985 - 1995 Anyone Who Wants It,
  7. //    All Rights Energetically Hurled as far away from me as possible.
  8. //    Use at your own (considerable) risk.
  9.  
  10.  
  11. #include "DialogUtils.h"
  12.  
  13. /* Most of this code is from C.K. Haun's DialogBits snippet, but of course has
  14. been tweaked for my own purposes */
  15.  
  16. // UPPs
  17. UserItemUPP        gBtnOutlineUPP;
  18. ModalFilterUPP    gStdKeyFilterUPP, gNumFilterUPP;
  19.  
  20. void InitDialogUPPs(void)
  21. {
  22.     // Build UPPs in heap
  23.     gBtnOutlineUPP = NewUserItemProc(BtnItem);
  24.     gStdKeyFilterUPP = NewModalFilterProc(StdKeyFilter);
  25.     gNumFilterUPP = NewModalFilterProc(NumFilter);
  26. }
  27.  
  28. /* This filter proc only allows numeric input, and also does the standard key
  29. filtering */
  30. pascal Boolean NumFilter(DialogPtr dptr, EventRecord *event, short *item)
  31. {
  32.     char         theKey;
  33.     WindowPtr     temp;
  34.     Boolean        returnVal = false;
  35.     
  36.     GetPort(&temp);
  37.     SetPort(dptr);
  38.     
  39.     //  Change the cursor to an I Beam if it's over the active editText item 
  40.     IBeamIt(dptr);
  41.     
  42.     //  Standard key filtering 
  43.     returnVal = StdKeyFilter(dptr, event, item);
  44.     
  45.     //  if that didn't handle it... 
  46.     if(returnVal == false)
  47.     {
  48.         //  We're only allowing numeric characters 
  49.         if ((event->what == keyDown) || (event->what == autoKey))
  50.         {
  51.             theKey = event->message & charCodeMask;
  52.             if(theKey > kLastCntrlKey && theKey < kDeleteKey)         //  Printable Ascii? 
  53.             {
  54.                 if (theKey < '0' || theKey > '9') //  not a number? 
  55.                 {
  56.                     SysBeep(1);            //  complain a little 
  57.                     returnVal = true;
  58.                 }
  59.             }
  60.             else 
  61.                 returnVal = false;
  62.         }
  63.     }
  64.     SetPort(temp);
  65.     return(returnVal);
  66. }
  67.  
  68. /* Standard key filtering: return or enter hit the OK button, escape hits cancel.
  69.     Also Hilite the button for visual feedback. */
  70. pascal Boolean StdKeyFilter(DialogPtr dptr, EventRecord *event, short *item)
  71. {
  72.     long         tilticks;
  73.     char         theKey;
  74.     Boolean        returnVal = false;
  75.  
  76.     if ((event->what == keyDown) || (event->what == autoKey))
  77.     {
  78.         theKey = event->message & charCodeMask;
  79.         switch (theKey)
  80.         {
  81.             //  return or enter hits the OK button 
  82.             case kReturnKey:
  83.             case kEnterKey:
  84.                 *item = iOKButton;
  85.                 //  now we need to invert the button 
  86.                 HiliteControl(SnatchHandle(dptr, iOKButton), inButton);
  87.                 Delay(8, &tilticks);        //  wait about 8 ticks so they can see it 
  88.                 HiliteControl(SnatchHandle(dptr, iOKButton), 0);
  89.                 returnVal = true;
  90.                 break;
  91.     
  92.             //  Escape hits the cancel button 
  93.             case kEscKey:
  94.                 *item = iCancelButton;
  95.                 HiliteControl(SnatchHandle(dptr, iCancelButton), inButton);
  96.                 Delay(8, &tilticks);        //  wait about 8 ticks so they can see it 
  97.                 HiliteControl(SnatchHandle(dptr, iCancelButton), 0);
  98.                 returnVal = true;
  99.                 break;
  100.             
  101.             default:
  102.                 break;
  103.         }
  104.     }
  105.     return returnVal;
  106. }       
  107.  
  108. void IBeamIt(WindowPtr dwind)
  109. {
  110.     Point     thePt;
  111.     short     kind;
  112.     Handle     itmhndl;
  113.     Rect     rect;
  114.     short     itemNum;
  115.     
  116.     //  first get the current edit line out of the dialog record 
  117.     itemNum = ((DialogPeek)dwind)->editField + 1;           //  always stored 1 less 
  118.     GetDItem(dwind, itemNum, &kind, &itmhndl, &rect);
  119.     GetMouse(&thePt);
  120.     if (PtInRect(thePt, &rect))
  121.     {
  122.         SetCursor(*(GetCursor(iBeamCursor)));
  123.     }
  124.     else
  125.     {
  126.         SetCursor(&qd.arrow);
  127.     }
  128. }
  129.  
  130. void ShortToDlog(short val, DialogPtr dptr, short item)
  131. {
  132.     short                kind;
  133.     Handle                itmhndl;
  134.     Rect                rect;
  135.     Str255                tempstr;
  136.     
  137.     NumToString((long)val, tempstr);
  138.     GetDItem(dptr, item, &kind, &itmhndl, &rect);
  139.     SetIText(itmhndl, tempstr);
  140. }
  141.  
  142. short DlogToShort(DialogPtr dptr, short itmnum)
  143. {
  144.     Handle            itmhndl;
  145.     Rect            rect;
  146.     Str255            tempstr;
  147.     short            kind;
  148.     long            temp;
  149.  
  150.     GetDItem(dptr, itmnum, &kind, &itmhndl, &rect);
  151.     GetIText(itmhndl, tempstr);
  152.     StringToNum(tempstr, &temp);
  153.     return (short)temp;
  154. }
  155.  
  156. pascal void BtnItem(DialogPtr dptr, short item)
  157. {
  158.     short            type;
  159.     Rect            rect;
  160.     Handle            hndl;
  161.     PenState        old;
  162.     
  163.     GetPenState(&old);
  164.     PenSize(3, 3);
  165.     GetDItem(dptr, iOKButton, &type, &hndl, &rect);
  166.     InsetRect(&rect, -4, -4);
  167.     FrameRoundRect(&rect, 16, 16);
  168.     SetPenState(&old);
  169. }    
  170.  
  171. /* Gets the ControlHandle for the item you want in the dialog box thebox.
  172.     Handy for setting checkboxes and radio buttons */
  173. ControlHandle SnatchHandle(DialogPtr thebox, short theGetItem)
  174. {
  175.     short itemtype;
  176.     Rect itemrect;
  177.     Handle thandle;
  178.     
  179.     GetDItem(thebox, theGetItem, &itemtype, &thandle, &itemrect);
  180.     return((ControlHandle)thandle);
  181. }
  182.  
  183. /* This routine checks the range of the number in the given item. If
  184.     it is out of range, the Range alert is invoked, the text is selected,
  185.     and false is returned.  */            
  186. Boolean    InRange(DialogPtr dptr, short theItem, short min, short max)
  187. {
  188.     short    type, theNum;
  189.     Rect    rect;
  190.     Handle    hndl;
  191.     Str255    minStr, maxStr;
  192.     Boolean rslt = true;
  193.     
  194.     GetDItem(dptr, theItem, &type, &hndl, &rect);
  195.     if(type == editText)
  196.     {
  197.         theNum = DlogToShort(dptr, theItem);
  198.         if(theNum < min || theNum > max)
  199.         {
  200.             NumToString((long)min, minStr);
  201.             NumToString((long)max, maxStr);
  202.             ParamText(minStr, maxStr, "\p", "\p");
  203.             StopAlert(kRangeAlert, nil);
  204.             SelIText(dptr, theItem, 0, 32767);
  205.             rslt = false;
  206.         }
  207.     }
  208.     return rslt;
  209. }
  210.     
  211.     
  212.